﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using System.Timers;
using Reborn_WorldServer;
using Reborn_WorldServer.App1AppHandler.Spells.Interfaces;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_FrozenFeetBuff : Buff, IRootBuff , IInstantCast
    {

        public int getTicksBase(SpellIcon sIcon)
        {
            if (sIcon.Name.IndexOf("Bolt") > -1)
                return 7;
            else if (sIcon.Name == "Frozen Feet")
                return 9;

            return 6;
        }

        public int getResistPenetration (SpellIcon spIcn)
        {
            if (spIcn.Name.IndexOf("Bolt") > -1)
                return 30;
            else if (spIcn.Name == "Frozen Feet")
                return 60;

            return 0;
        }


        public Spell_FrozenFeetBuff() : base()
        {
            minutesLeft = -2;
            spellTimer.Interval = 3000;
        }

        // (Level 15)
        private int IceBaseDamageLowRank2 = 240;
        private int IceBaseDamageHighRank2 = 260;
        //Ice//

        public string RootMessage()
        {
            return "Your feet are frozen!";
        }


        override public void Resume(CreatureBase targetCreatureBase, SpellIcon _spellIcon, int timeRemaining, int castLevel, int castCharacterID, int castInt, int castWis, int optionalValue, string xmlData)
        {
            if (timeRemaining == -1)
            {
                this.spellIcon = _spellIcon;
                timeRemaining = getTicksBase(_spellIcon);
            }
            
            ticksLeft = timeRemaining;
            
            base.Resume(targetCreatureBase, _spellIcon, timeRemaining, castLevel, castCharacterID, castInt, castWis, optionalValue, xmlData);

            foreach (Buff buff in TargetCreatureBase.buffManager.Buffs)
            {
                if (buff.spellIcon.StackType == base.spellIcon.StackType && buff.SpellID != SpellID)
                {
                    buff.RemoveSpell(false, false);
                }
            }

            spellTimer.Start();
        }

        bool castComplete = false;
        override public bool tryResist(SpellIcon _spellIcon, int castLevel, int castInt, int castWis, CreatureBase targetCreatureBase)
        {
            if ((_spellIcon.Name == "Frozen Foot" || _spellIcon.Name == "Frozen Feet") || (_spellIcon.Name.IndexOf("Bolt") > -1 && castComplete == true))
            {
                int counter = ticksLeft;
                if (ticksLeft < 1)
                    counter = getTicksBase(_spellIcon);

                int penetration = SkillManager.AdjustForCombat(castLevel, getResistPenetration(_spellIcon) * (counter / 2), castInt);

                int resist = targetCreatureBase.getResist(_spellIcon.Skill);

                int result = Managers.GameHandler.random.Next(1, penetration + resist + 1);
                if (result > penetration)
                    return true;
            }

            return false;
        }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, false, out MapItemsWithin12) == false)
                return;

            if (spellIcon.Name == "Frozen Foot")
            {
                TargetCreatureBase.SendMessage(myCreatureBase.Name + "casts " + spellIcon.Name + " on you!", "spelldamaged");
                myCreatureBase.SendMessage("You cast " + spellIcon.Name + " on " + TargetCreatureBase.Name + "!", "spelldamage");
            }
            if (spellIcon.Name == "Freezing Bolt")
            {
                List<Character> CharactersInUpdateRange = new List<Character>();
                foreach (MapItem mapitem in MapItemsWithin12)
                {
                    foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                    {
                        if (creaturebase is Character)
                            CharactersInUpdateRange.Add((Character)creaturebase);
                    }
                }

                int DamageAmount = myCreatureBase.GameHandler.random.Next(IceBaseDamageLowRank2, IceBaseDamageHighRank2 + 1);
                DamageAmount = SkillManager.AdjustForCombat(CastLevel, DamageAmount, myCreatureBase.IntelligenceTotal);
                DamageAmount = TargetCreatureBase.CalculateGetSpellDamagePerResists(DamageAmount, spellIcon.Skill);

                int absorbedDamage;
                TargetCreatureBase.InflictSpellDamage(DamageAmount,
                    myCreatureBase is Character ? myCreatureBase.ID : -1,
                    myCreatureBase is Creature ? myCreatureBase.CreatureID : -1,
                    spellIcon.Skill, out absorbedDamage);

                TargetCreatureBase.Update_Character();

                DamageAmount = DamageAmount - absorbedDamage;

                if (myCreatureBase is Character)
                {
                    Character myCharacter = (Character)myCreatureBase;

                    Message healMsg1 = new Message();
                    healMsg1.CreatureID = myCharacter.CreatureID;
                    healMsg1.Type = "damage";
                    if (spellIcon.Skill == "Fire")
                        healMsg1.Text = "You burn " + TargetCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Ice")
                        healMsg1.Text = "You freeze " + TargetCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Unholy")
                        healMsg1.Text = "You drain " + TargetCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Mentality")
                        healMsg1.Text = "You shock " + TargetCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Holy")
                        healMsg1.Text = "You smite " + TargetCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Unholy")
                        healMsg1.Text = "You sap " + TargetCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";

                    if (absorbedDamage != 0)
                        healMsg1.Text = healMsg1.Text + " ABSORBED " + absorbedDamage.ToString();

                    myCharacter.AddMessage(healMsg1);
                }
                if (TargetCreatureBase is Character && TargetCreatureBase != myCreatureBase)
                {
                    Character targetCharacter = (Character)TargetCreatureBase;

                    Message healMsg1 = new Message();
                    healMsg1.CreatureID = targetCharacter.CreatureID;
                    healMsg1.Type = "damaged";
                    if (spellIcon.Skill == "Fire")
                        healMsg1.Text = myCreatureBase.Name + " burns you! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Ice")
                        healMsg1.Text = myCreatureBase.Name + " freezes you! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Unholy")
                        healMsg1.Text = myCreatureBase.Name + " drains you! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Mentality")
                        healMsg1.Text = myCreatureBase.Name + " shocks you! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Holy")
                        healMsg1.Text = myCreatureBase.Name + " smites you! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Unholy")
                        healMsg1.Text = myCreatureBase.Name + " saps you! (" + DamageAmount.ToString() + ")";

                    if (absorbedDamage != 0)
                        healMsg1.Text = healMsg1.Text + " ABSORBED " + absorbedDamage.ToString();

                    targetCharacter.AddMessage(healMsg1);
                }

                List<Character> CharsToUpdate = new List<Character>();
                MapItem mapitemToUpdate = TargetCreatureBase.currentMapItem;
                foreach (MapItem mapitem in MapItemsWithin12)
                {
                    if (Math.Abs(mapitem.MapX - myCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - myCreatureBase.MapY) <= 4)
                    {
                        foreach (CreatureBase cb in mapitem.CreaturesBases)
                        {
                            if (cb is Character)
                            {
                                CharsToUpdate.Add((Character)cb);
                            }
                        }
                    }
                    if (Math.Abs(mapitem.MapX - myCreatureBase.MapX) <= 8 && Math.Abs(mapitem.MapY - myCreatureBase.MapY) <= 8)
                    {
                        foreach (CreatureBase cb in mapitem.CreaturesBases)
                        {
                            if (cb is Character)
                            {
                                Character character = (Character)cb;
                                if (spellIcon.Skill == "Fire")
                                    character.PlaySound("FireBolt", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                                else if (spellIcon.Skill == "Ice")
                                    character.PlaySound("IceBolt", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                                else if (spellIcon.Skill == "Mentality")
                                    character.PlaySound("MindShock", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                                else if (spellIcon.Skill == "Holy")
                                    character.PlaySound("Smite", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                                else if (spellIcon.Skill == "Unholy")
                                    character.PlaySound("SoulSap", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                            }
                        }
                    }
                    if (TargetCreatureBase.HP < 1 && mapitem.MapX == TargetCreatureBase.MapX && mapitem.MapY == TargetCreatureBase.MapY)
                    {
                        if (!(TargetCreatureBase is Character))
                        {
                            mapitem.CreaturesBases.Remove(TargetCreatureBase);
                        }
                        mapitemToUpdate = mapitem;
                    }
                }
                foreach (Character character in CharsToUpdate)
                {
                    if (TargetCreatureBase.HP < 1)
                        character.AddMapItem(mapitemToUpdate);
                    else
                        character.UpdateCreatureBaseHp(TargetCreatureBase);
                }
            }
            castComplete = true;

            bool resistFreeze = tryResist(spellIcon, CastingCreatureLevel, CastingCreatureInt, CastingCreatureWis, TargetCreatureBase);
            if (TargetCreatureBase.HP > 0 && resistFreeze == false)
            {
                TargetCreatureBase.buffManager.UpdateBuffs();

                if (overwrittenBuff != null && spellIcon.Name == "Freezing Bolt")
                    ticksLeft = overwrittenBuff.ticksLeft;
                else
                    ticksLeft = getTicksBase(spellIcon);

                spellTimer.Start();
            }
            else if (TargetCreatureBase.HP > 0 && resistFreeze == true)
            {
                if (myCreatureBase != TargetCreatureBase)
                    myCreatureBase.SendMessage(TargetCreatureBase.Name + " resists " + spellIcon.Name + "!", "spelldefense");

                TargetCreatureBase.SendMessage("You resist " + myCreatureBase.Name + "'s " + spellIcon.Name + "!", "spelloffense");

                RemoveSpell(true, false);
            }
        }

        override protected void SpellTimer_Expired(object source, ElapsedEventArgs e)
        {
            ticksLeft--;

            LockItem lockitem = TargetCreatureBase.GameHandler.GetLockItem(TargetCreatureBase.MapID, TargetCreatureBase.Instance);
            lock (lockitem.LockObj)
            {
                //This buff has been removed, do nothing (this can occur if the buff gets overwritten exactly when this timer fires)
                if (TargetCreatureBase.buffManager.hasBuff(this.SpellID) == false)
                    return;

                if (TargetCreatureBase.HP < 1)
                {
                    RemoveSpell(true, false);
                    return;
                }

                if (tryResist(spellIcon, CastingCreatureLevel, CastingCreatureInt, CastingCreatureWis, TargetCreatureBase) == true)
                {
                    RemoveSpell(true, true);
                    return;
                }

                if (ticksLeft == 0)
                {
                    RemoveSpell(true, true);
                    return;
                }
                else
                    spellTimer.Start();
            }
            
        }

        public void InstantCast(CreatureBase castingCreature, CreatureBase targetCreatureBase, SpellIcon _spellIcon, int castLevel)
        {
            if (InitializeInstantCast(castingCreature, targetCreatureBase, _spellIcon, castLevel, false) == false)
                return;

            List<Character> charactersWithin8 = new List<Character>();
            foreach (MapItem mapItem in TargetCreatureBase.currentMapItem.mapItemsWithin8)
            {
                foreach (CreatureBase creatureBase in mapItem.CreaturesBases)
                {
                    if (creatureBase is Character)
                        charactersWithin8.Add((Character)creatureBase);
                }
            }

            if (spellIcon.Name == "Frozen Foot")
            {
                TargetCreatureBase.SendMessage(castingCreature.Name + "casts " + spellIcon.Name + " on you!", "spelldamaged");
                castingCreature.SendMessage("You cast " + spellIcon.Name + " on " + TargetCreatureBase.Name + "!", "spelldamage");
            }
            if (spellIcon.Name == "Freezing Bolt")
            {
                int DamageAmount = Managers.GameHandler.random.Next(IceBaseDamageLowRank2, IceBaseDamageHighRank2 + 1);
                DamageAmount = SkillManager.AdjustForCombat(CastingCreatureLevel, DamageAmount, CastingCreatureInt);
                DamageAmount = TargetCreatureBase.CalculateGetSpellDamagePerResists(DamageAmount, spellIcon.Skill);

                int absorbedDamage;
                TargetCreatureBase.InflictSpellDamage(DamageAmount, CastingCharacterID, CastingCreatureGUID, spellIcon.Skill, out absorbedDamage); 

                TargetCreatureBase.Update_Character();

                DamageAmount = DamageAmount - absorbedDamage;

                if (castingCreature is Character)
                {
                    Character myCharacter = (Character)castingCreature;

                    Message healMsg1 = new Message();
                    healMsg1.CreatureID = myCharacter.CreatureID;
                    healMsg1.Type = "damage";
                    if (spellIcon.Skill == "Fire")
                        healMsg1.Text = "You burn " + TargetCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Ice")
                        healMsg1.Text = "You freeze " + TargetCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Unholy")
                        healMsg1.Text = "You drain " + TargetCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Mentality")
                        healMsg1.Text = "You shock " + TargetCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Holy")
                        healMsg1.Text = "You smite " + TargetCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Unholy")
                        healMsg1.Text = "You sap " + TargetCreatureBase.Name + "! (" + DamageAmount.ToString() + ")";

                    if (absorbedDamage != 0)
                        healMsg1.Text = healMsg1.Text + " ABSORBED " + absorbedDamage.ToString();

                    myCharacter.AddMessage(healMsg1);
                }
                if (TargetCreatureBase is Character && TargetCreatureBase != castingCreature)
                {
                    Character targetCharacter = (Character)TargetCreatureBase;

                    Message healMsg1 = new Message();
                    healMsg1.CreatureID = targetCharacter.CreatureID;
                    healMsg1.Type = "damaged";
                    if (spellIcon.Skill == "Fire")
                        healMsg1.Text = castingCreature.Name + " burns you! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Ice")
                        healMsg1.Text = castingCreature.Name + " freezes you! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Unholy")
                        healMsg1.Text = castingCreature.Name + " drains you! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Mentality")
                        healMsg1.Text = castingCreature.Name + " shocks you! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Holy")
                        healMsg1.Text = castingCreature.Name + " smites you! (" + DamageAmount.ToString() + ")";
                    else if (spellIcon.Skill == "Unholy")
                        healMsg1.Text = castingCreature.Name + " saps you! (" + DamageAmount.ToString() + ")";

                    if (absorbedDamage != 0)
                        healMsg1.Text = healMsg1.Text + " ABSORBED " + absorbedDamage.ToString();

                    targetCharacter.AddMessage(healMsg1);
                }

                foreach (Character character in charactersWithin8)
                {
                    if (spellIcon.Skill == "Fire")
                        character.PlaySound("FireBolt", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                    else if (spellIcon.Skill == "Ice")
                        character.PlaySound("IceBolt", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                    else if (spellIcon.Skill == "Mentality")
                        character.PlaySound("MindShock", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                    else if (spellIcon.Skill == "Holy")
                        character.PlaySound("Smite", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                    else if (spellIcon.Skill == "Unholy")
                        character.PlaySound("SoulSap", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
                }
            }
            
            castComplete = true;

            if (TargetCreatureBase.HP < 1)
            {
                if (TargetCreatureBase is Creature)
                    TargetCreatureBase.currentMapItem.CreaturesBases.Remove(TargetCreatureBase);

                foreach (Character character in charactersWithin8)
                    character.AddMapItem(TargetCreatureBase.currentMapItem);

                return;
            }

            castComplete = true;

            bool resistFreeze = tryResist(spellIcon, CastingCreatureLevel, CastingCreatureInt, CastingCreatureWis, TargetCreatureBase);
            if (TargetCreatureBase.HP > 0 && resistFreeze == false)
            {
                TargetCreatureBase.buffManager.UpdateBuffs();

                if (overwrittenBuff != null && spellIcon.Name == "Freezing Bolt")
                    ticksLeft = overwrittenBuff.ticksLeft;
                else
                    ticksLeft = getTicksBase(spellIcon);

                spellTimer.Start();
            }
            else if (TargetCreatureBase.HP > 0 && resistFreeze == true)
            {
                if (castingCreature != TargetCreatureBase)
                    castingCreature.SendMessage(TargetCreatureBase.Name + " resists " + spellIcon.Name + "!", "spelldefense");

                TargetCreatureBase.SendMessage("You resist " + castingCreature.Name + "'s " + spellIcon.Name + "!", "spelloffense");

                RemoveSpell(true, false);
            }
           
        }
    }
}
